#include "livestatusitemdelegate.h"
#include "core/model/modeltype.h"
#include "liverecordertype.h"
#include "utils/utils.h"
#include <QApplication>
#include <QFont>
#include <QPainter>
#include <QStyle>
#include <QStyleOptionButton>

LiveStatusItemDelegate::LiveStatusItemDelegate(QObject* parent) : QStyledItemDelegate(parent) { }

void LiveStatusItemDelegate::paint(QPainter* painter, const QStyleOptionViewItem& option,
                                   const QModelIndex& index) const
{
    QStyledItemDelegate::paint(painter, option, index);
    painter->setRenderHints(QPainter::Antialiasing | QPainter::TextAntialiasing
                            | QPainter::SmoothPixmapTransform);
    painter->save();

    const int live_status = index.data(LiveDataRole::LIVE_STATUS_ROLE).toInt();
    const int record_status = index.data(RECORD_STATUS_ROLE).toInt();
    if (live_status == yc::ACTIVE_LIVE_STATUS || record_status == RECORDING_STATUS
        || record_status == MERGEING_STATUS) {
        // // 粗体
        // QFont font = painter->font();
        // font.setBold(true);
        // painter->setFont(font);

        QRect rect { QPoint(option.rect.left() + 4,
                            option.rect.top() + option.rect.height() / 2 - 15),
                     QSize(74, 30) };

        // 直播中(#EF318F)
        painter->setBrush(QBrush(QColor(0xEF318F)));
        painter->setPen(Qt::NoPen);
        painter->drawRoundedRect(rect, 3, 3);
        painter->setPen(QPen(Qt::white, 1));
        painter->drawText(rect, Qt::TextSingleLine | Qt::AlignCenter, tr("Living"));

        // 在线观众 - 无背景色，使用次要文本色
        rect.moveLeft(rect.right() + 4);
        rect.setWidth(116);
        painter->setPen(QPen(QColor(0x5A6A7E)));
        const int audience_count = index.data(AUDIENCE_COUNT_ROLE).toInt();
        QString text;
        if (audience_count >= 10000 && audience_count % 10000 == 0) {
            text = tr("Audience: %1w").arg(audience_count / 10000);
        } else {
            text = tr("Audience: %1").arg(audience_count);
        }
        painter->drawText(rect, Qt::TextSingleLine | Qt::AlignLeft | Qt::AlignVCenter, text);

        // 录制中/合成中
        if (record_status == RECORDING_STATUS || record_status == MERGEING_STATUS) {
            rect.moveLeft(rect.right() + 4);
            rect.setWidth(74);
            painter->setBrush(QBrush(QColor(0x34A853)));
            painter->setPen(Qt::NoPen);
            painter->drawRoundedRect(rect, 3, 3);
            painter->setPen(QPen(Qt::white, 1));
            painter->drawText(rect, Qt::TextSingleLine | Qt::AlignCenter,
                              record_status == RECORDING_STATUS ? tr("Recording") : tr("Merging"));

            // 录制时长 - 无背景色，使用次要文本色
            qint64 duration = index.data(RECORD_DURATION_ROLE).toLongLong();
            rect.moveLeft(rect.right() + 4);
            rect.setWidth(116);
            painter->setPen(QPen(QColor(0x5A6A7E)));
            painter->drawText(rect, Qt::TextSingleLine | Qt::AlignLeft | Qt::AlignVCenter,
                              tr("Duration: %1").arg(utils::clockDisplayString(duration)));

            if (record_status == MERGEING_STATUS) {
                rect.moveLeft(rect.right() + 4);
                rect.setWidth(30);
                const QPixmap pixmap(
                        QString(":/images/loading/72px_%1.png")
                                .arg(index.data(LOADING_INDEX_ROLE).toInt(), 0, 10, QChar('0')));
                painter->drawPixmap(rect, pixmap, QRect(QPoint(0, 0), pixmap.size()));
            }
        }

        // 消息
        const QString message = index.data(MESSAGE_ROLE).toString();
        if (!message.isEmpty()) {
            rect.moveLeft(rect.right() + 4);
            rect.setRight(option.rect.right() - 4);
            painter->setBrush(QBrush(QColor(0xEA4335)));
            painter->setPen(Qt::NoPen);
            painter->drawRoundedRect(rect, 3, 3);
            painter->setPen(QPen(Qt::white, 1));
            painter->drawText(rect, Qt::TextSingleLine | Qt::AlignLeft | Qt::AlignVCenter, message);
        }
    } else {
        const QRect rect { QPoint(option.rect.left() + 4,
                                  option.rect.top() + option.rect.height() / 2 - 15),
                           QSize(90, 30) };
        if (index.data(LiveDataRole::AUTO_CHECK_ROLE).toInt() == 1) {
            // 自动检测中
            // QFont font = painter->font();
            // font.setBold(true);
            // painter->setFont(font);
            painter->setBrush(QBrush(QColor(0x76C7C0)));
            painter->setPen(Qt::NoPen);
            painter->drawRoundedRect(rect, 3, 3);
            painter->setPen(QPen(Qt::white, 1));
            painter->drawText(rect, Qt::TextSingleLine | Qt::AlignCenter, tr("Checking..."));
        } else {
            painter->setPen(QPen(Qt::black));
            painter->drawText(rect, Qt::TextSingleLine | Qt::AlignLeft | Qt::AlignVCenter,
                              QString("--"));
        }
    }

    painter->restore();
}
